//
//  TSTaskSearchViewController.m
//  chart
//
//  Created by xu on 15/8/16.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSTaskSearchViewController.h"
#import "TSDatePickerTextField.h"
#import "TSToolBarTextField.h"
#import "TSPickerTextField.h"
#import "TSMsgSearchResultTableViewController.h"


#define SEARCH_LABEL_WIDTH  100.f
#define SEARCH_LABEL_HEIGHT 35.f
#define SEARCH_ROW_GAP 10.f

@interface TSTaskSearchViewController ()

@end

@implementation TSTaskSearchViewController{
    TSDatePickerTextField *startTimePickerTextField;
    TSDatePickerTextField *endTimePickerTextField;
    TSToolBarTextField *sectionTextField;
    TSPickerTextField *typePickerTextField;
    UITextField *keywordTextField;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"任务查询";
    
    [self addTapGesture];
    
    [self addTimeRow];
    
    [self addSectionRow];
    
    [self addTypeRow];
    
    [self addKeyworkRow];
    
    [self addSearchBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)addTapGesture
{
    /* 加入收键盘手势 */
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleField:)];
    [self.view addGestureRecognizer:tap];
}

/* 隐藏键盘 */
- (void)handleField:(UIGestureRecognizer*)gesture{
    for (UIView * view in self.view.subviews) {
        if ([view isFirstResponder]) {
            [view resignFirstResponder];
        }
    }
}

- (UILabel *)createLabel:(CGRect)frame Title:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:title];
    [label setFont:FONT(12.f)];
    
    return label;
}

- (void)addTimeRow
{
    UILabel *startL_label = [self createLabel:CGRectMake(MARGIN_LEFT, MARGIN_TOP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"开始时间:"];
    [self.view addSubview:startL_label];
    
    startTimePickerTextField = [[TSDatePickerTextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(startL_label.frame)+10.f, MARGIN_TOP, currentDeviceWidth-MARGIN_LEFT*2-SEARCH_LABEL_WIDTH-10.f, SEARCH_LABEL_HEIGHT)];
    startTimePickerTextField.layer.borderWidth = 1.f;
    [self.view addSubview:startTimePickerTextField];
    
    UILabel *end_label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(startL_label.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"结束时间:"];
    [self.view addSubview:end_label];
    endTimePickerTextField = [[TSDatePickerTextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(end_label.frame)+10.f, CGRectGetMinY(end_label.frame), CGRectGetWidth(startTimePickerTextField.frame), SEARCH_LABEL_HEIGHT)];
    endTimePickerTextField.layer.borderWidth = 1.f;
    [self.view addSubview:endTimePickerTextField];
}

- (void)addSectionRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(endTimePickerTextField.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"部门:"];
    [self.view addSubview:label];
    
    sectionTextField = [[TSToolBarTextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), CGRectGetWidth(startTimePickerTextField.frame), SEARCH_LABEL_HEIGHT)];
    sectionTextField.layer.borderWidth = 1.f;
    [self.view addSubview:sectionTextField];
}

- (void)addTypeRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(sectionTextField.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"类型:"];
    [self.view addSubview:label];
    
    typePickerTextField = [[TSPickerTextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), CGRectGetWidth(startTimePickerTextField.frame), SEARCH_LABEL_HEIGHT) WithSourceArray:@[@"全部", @"接收消息", @"发送消息", @"个人日程"]];
    typePickerTextField.layer.borderWidth = 1.f;
    [self.view addSubview:typePickerTextField];
}

- (void)addKeyworkRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(typePickerTextField.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"关键字:"];
    [self.view addSubview:label];
    
    keywordTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), CGRectGetWidth(startTimePickerTextField.frame), SEARCH_LABEL_HEIGHT)];
    keywordTextField.layer.borderWidth = 1.f;
    [self.view addSubview:keywordTextField];
}

- (void)addSearchBtn
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(keywordTextField.frame)+SEARCH_ROW_GAP, currentDeviceWidth-2*MARGIN_LEFT, SEARCH_LABEL_HEIGHT)];
    [btn setTitle:@"查询" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onSearchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundColor:[UIColor lightGrayColor]];
    [btn.layer setCornerRadius:5.f];
    [btn.titleLabel setFont:FONT(12.f)];
    [self.view addSubview:btn];
}

- (void)onSearchBtnClick:(UIButton *)sender
{
    TSMsgSearchResultTableViewController *search_result_vc = [[TSMsgSearchResultTableViewController alloc] init];
    [self.navigationController pushViewController:search_result_vc animated:YES];
}





@end
